var global_count = 1;

$(document).ready( 
	function()
	{
	
		$("#search_for_list > input[name=search]").click( search_list );
		$("#list_submit").click( submit_list );
		$("#item_list a[tag=delete_item]").bind( "click", function(){
				$(this).parent().remove();
				global_count--;
				});
				
		
		var start = $("#item_list div:last-child input[tag=priority]").val();
		if( start )
			global_count = parseInt(start) + 1;
			
		if(global_count>1)
			$("#item_list_content").show();
			
		//tab切换 2010.09.08	
		$(".add_list_menu li").bind( "mouseover",setltab);	
   		SetFocus1();
        var str = $('#tburl').val();
        if( "" == str ){
                $("#tburl").val("请输入要添加的店铺或商品的链接地址");
        }
        else{
                $("#tburl").val( str );
        }
        
        //点击通过url添加 2010.09.08
        $("#addtburlbtn").click( add_url_list);	
               	
        //按回车直接搜索 2010.09.08
   		$("#keyword").bind( "keydown", {button: $('#searchbtn')}, PressKeyDownS );		
   		
	}
);

function PressKeyDownS(event){

	if(event.keyCode==13){
		var button=event.data.button;
		button.click();
		return false;
	}
}

function showMessage( msg )
{
	$("#msgbox").html( msg );
	window.scroll(0,0);
}

function submit_list()
{
	var content = "";
	var priority = "";
	var cate = "";
		
	$("#item_list input[tag=id]").each(function(){
		content += $(this).val() + ",";
	});
	
	$("#item_list input[tag=priority]").each(function(){
		priority += $(this).val() + ",";
	});
	
	$("#item_list input[tag=cate]").each(function(){
		cate += $(this).val() + ",";
	});
	
	$("#list_content").val( content );
	$("#list_priority").val( priority );
	$("#list_cate").val( cate );
	
	$("form[name=user_list]")[0].submit();
}

function search_list( obj )
{
	
	 var params = $('#search_for_list').serialize();
	 $("#search_result").show();
     $.ajax(
   	 {
   		 type: 'get',
         url: edp_domain_url + 'ajax_search_in_list/?tiny=1&' + params,
		 timeout: 5000,
		 global:false,
	     beforeSend: function()
	     {
   		 	$("#search_result").text("正在搜索...");
		 },
		 error: function()
		 {
			$("#search_result").text("发生错误，稍后再试...");
		 },
         success: function( data, textStatus )
         {
			$("#search_result").html( data );
			setup_link_after_load();
         }
     });   
}



function search_list_page( addr )
{
     $.ajax(
   	 {
   		 type: 'get',
         url: addr,
		 timeout: 5000,
		 global:false,
	     beforeSend: function()
	     {
   		 	$("#search_result").text("正在搜索...");
		 },
		 error: function()
		 {
			$("#search_result").text("发生错误，稍后再试...");
		 },
         success: function( data, textStatus )
         {
			$("#search_result").html( data );
			setup_link_after_load();
         }
     });
}

function setup_link_after_load()
{
	$(".pagebox a").bind("click",
			function(event)
			{
				addr = $(this).attr('href');
				event.preventDefault();
				search_list_page( addr );
			}
	);
	
	$("#search_result a[id]").each(
			function()
			{
				$(this).bind( "click", function(){
					add_item_to_list( $(this).attr('id'), $(this).siblings().filter('table').find('span[tag=item_name]').text() );
				});
			}
	);
	
	$(".list_entry img").each(
		function()
		{
			if( $(this).width() > 80 )
				$(this).width(80);
		}
	);
	
}


function delete_item( obj )
{
	$(obj).closest('div').remove();
	global_count--;
	if(global_count==1){
 		$("#item_list_label").hide();
 	}	
}

function add_item_to_list( item_id,  desc )
{
	if( $("#item_list input[tag=id]").filter("[value=" + item_id + "]").length == 1 )
	{
		alert("该项已添加至评单，请重新选择！");
		return;
	}
	
	var cate = ( $("#search_cate").val() == "shop" || $("#search_cate").val() == 'mall' ) ? 0 : 1;
	
	var content = "<div><table width=\"100%\"><tr><td width=\"30px\"><input tag=\"priority\" type=\"text\" value=\"" + global_count + 
		"\"/><input type=\"hidden\" tag=\"id\" value=\"" + item_id +  "\"/>" +
		"<input type=\"hidden\" value=\"" + cate + "\" tag=\"cate\" /></td><td align=\"left\">" +
		desc + "</td><td align=\"right\" width=\"30px\"><a href=\"#\" tag=\"delete_item\" onclick=\"delete_item(this);\">删除</a></td></tr></table></div>";
	
	global_count++;
	$(content).appendTo("#item_list");
	
	if(global_count>1){
		$("#item_list_content").show();
	}
			
}



//add by bo 2010.09.08
function setltab(){

	if($(this).attr("class")=="tab_show"){
	
		$(this).removeClass("tab_show");
		$(this).addClass("tab_current");
		
		if($(this).attr("id")=="t1"){
			$("#t2").removeClass("tab_current");
			$("#t2").addClass("tab_show");
			$("#alb2").hide();
			$("#alb1").show();
		}
		
		if($(this).attr("id")=="t2"){
			$("#t1").removeClass("tab_current");
			$("#t1").addClass("tab_show");
			$("#alb1").hide();
			$("#alb2").show();					
		}		
	}

}
//add by bo 2010.09.08
function SetFocus1(){
        //      var str = $("#keyword").val();
        //when search input box set to focus, clear the field
        $("#tburl").focus(
                function(){
                        if( $("#tburl").val() == "请输入要添加的店铺或商品的链接地址" ){
                                $("#tburl").val("");
                        }
                }
        )
        $("#tburl").blur(
                function(){
                        if( $("#tburl").val() == "" ){
                                $("#tburl").val("请输入要添加的店铺或商品的链接地址");
                        }
                }
        )
}

//add by bo 2010.09.08
function add_url_list( obj )
{
	
	 var params = $('#add_url_list').serialize();
	 $("#add_url_result").show();
	 $("#addtburlbtn").attr("disabled","true");
     $.ajax(
   	 {
   		 type: 'get',
         url: edp_domain_url + 'ajax_add_url_in_list/?tiny=1&' + params,
		 timeout: 50000,
		 dataType: 'json',	
		 global:false,
	     beforeSend: function()
	     {
   		 	$("#add_url_result").text("正在添加,请稍等...");
		 },
		 error: function()
		 {
			$("#add_url_result").text("发生错误，稍后再试...");
         	$("#addtburlbtn").removeAttr("disabled");			
		 },	 
         success: function( data, textStatus )
         {
         	
			$("#add_url_result").html( data.text);
			
			if(data.result=="true")
				add_item_to_list_by_url( data.id, data.name);
         
         	$("#addtburlbtn").removeAttr("disabled");
         }
     });   
}

//add by bo 2010.09.08
function add_item_to_list_by_url( item_id,  desc )
{
	if( $("#item_list input[tag=id]").filter("[value=" + item_id + "]").length == 1 )
	{
		alert("该项已添加至评单，请重新选择！");
		return;
	}
	
	var cate = ( $("#add_url_cate").val() == "shop") ? 0 : 1;
	
	var content = "<div><table width=\"100%\"><tr><td width=\"30px\"><input tag=\"priority\" type=\"text\" value=\"" + global_count + 
		"\"/><input type=\"hidden\" tag=\"id\" value=\"" + item_id +  "\"/>" +
		"<input type=\"hidden\" value=\"" + cate + "\" tag=\"cate\" /></td><td align=\"left\">" +
		desc + "</td><td align=\"right\" width=\"30px\"><a href=\"###\" tag=\"delete_item\" onclick=\"delete_item(this);\">删除</a></td></tr></table></div>";
	
	global_count++;
	$(content).appendTo("#item_list");
	
	if(global_count>1){
		$("#item_list_content").show();
	}
			
}